﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FightTheDragon.Managers;
using FightTheDragon.GameObjects;
using FightTheDragon.Interface;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace FightTheDragon.States
{
    /// <summary>
    /// Main Menu State Representing the Various Options
    /// </summary>
    class MMState : BaseState
    {


        private Text m_PressStartText;
        private Text m_MainMenuText;
        private Image m_Back;
        private Cursor m_Cursor;
        private bool ControllerCheckOnce;
        private InterfaceManager m_Interface;
        private InputManager m_Input;
        private StateManager m_State;
        public enum States { PLAY = 2, OPTIONS, SANDBOX, HIGHSCORE, CREDITS, EXIT };
        public MMState()
        {


            m_MainMenuText = new Text();
            m_PressStartText = new Text();
            m_Cursor = new Cursor();
            m_Back = new Image();
            ControllerCheckOnce = false;
            m_Interface = null;
            m_Input = null;
            m_State = null;


        }

        void BaseState.Enter()
        {
            ControllerCheckOnce = false;
            m_MainMenuText = new Text(1280 / 2, 720 / 2, 0, "SpriteFont1", "ORBITAL\nPlay\nOptions\nSandBox\nHighScores\nCredits\nExit", 1, Color.Red, 1, new RandEffect());
            m_PressStartText = new Text(1280 / 2, 720 / 2, 1, "SpriteFont1", "PRESS START", 1, Color.Red, 1, new RotateEffect(false));
            m_Cursor = new Cursor(0, "MMeteor", .3f, Color.White, 1.0f, m_MainMenuText, 2, 7);
            m_Interface = InterfaceManager.Instance;
            m_Input = InputManager.Instance;
            m_State = StateManager.Instance;
            m_Interface.Add(m_PressStartText);

        }

        void BaseState.Exit()
        {
            m_Interface.Remove(m_PressStartText);
            m_Interface.Remove(m_MainMenuText);
            m_Interface.Remove(m_Cursor);

        }
        //Input is acceptable for both the Controller and the Keyboard atm...
        bool BaseState.HandleInput()
        {

            if ((m_Input.DetermineController() && !ControllerCheckOnce) || m_Input.KeyPressedOnce(Keys.Enter) && !ControllerCheckOnce)
            {
                m_Interface.Remove(m_PressStartText);
                m_Interface.Add(m_MainMenuText);
                m_Interface.Add(m_Cursor);
                ControllerCheckOnce = true;
            }
            else
            {

                if (m_Input.ButtonPressedOnce(Buttons.LeftThumbstickDown) || m_Input.KeyPressedOnce(Keys.Down))
                    m_Cursor.CursorMovement++;

                if (m_Input.ButtonPressedOnce(Buttons.LeftThumbstickUp) || m_Input.KeyPressedOnce(Keys.Up))
                    m_Cursor.CursorMovement--;

                if (m_Input.ButtonPressedOnce(Buttons.A) || m_Input.KeyPressedOnce(Keys.Enter))
                {
                    switch (m_Cursor.CursorMovement)
                    {

                        case (int)States.PLAY:
                            m_State.ChangeState(new GameplayState());
                            break;
                        case (int)States.OPTIONS:
                            break;
                        case (int)States.SANDBOX:
                            break;
                        case (int)States.HIGHSCORE:
                            break;
                        case (int)States.CREDITS:
                            m_State.ChangeState(new CreditsState());
                            break;
                        case (int)States.EXIT:
                            break;
                    }
                }
            }



            return true;
        }
        void BaseState.Render()
        {
            m_Interface.Render();

        }
        void BaseState.Update(float gTime)
        {
            m_Input.Update();
            m_Interface.Update(gTime);
        }

    }
}
